package com.system.controller;

import com.system.po.CourseCustom;
import com.system.service.CourseService;
import com.system.service.UserloginService;
import com.system.service.impl.CourseServiceImpl;
import com.system.utils.SendEmailUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 定时任务A
 * @author Administrator
 * @date 2017-11-23 下午 15:24
 */
@Controller
public class JobA implements Job{

    private CourseService courseService;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("开始执行定时任务A...");
        try {
            //发送邮件
            if (courseService == null) {
                //获取service
                courseService = (CourseService) ApplicationContextUtil.getBean("courseServiceImpl");
            }
            Date date = new Date();
            List<CourseCustom> byCourseTime = courseService.findByCourseTime(simpleDateFormat.format(date));
            for (CourseCustom courseCustom: byCourseTime) {
                //收件人邮箱
                String email = courseCustom.getCourseweek();
                //信件名称
                //信件名称
                String title = courseCustom.getCoursename();
                //信件内容
                String content = courseCustom.getClassroom();
                SendEmailUtil.sendEmail(title, content, email);
                //发送成功之后更新当前发送状态
                courseCustom.setCoursetype("1");
                courseService.upadteById(courseCustom.getCourseid(), courseCustom);
            }

        }catch (Exception e) {
            System.out.println("执行定时任务A失败");
        }

        System.out.println("结束执行定时任务A...");
    }
}